/**
 * @author Fang Lu, fanglupku@gmail.com
 * 2012-2-7
 */
package com.autotag.data.crawler;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class SimpleHTMLFetcher {
	private HttpClient httpClient = new DefaultHttpClient();

	/**
	 * 
	 * @param urlString
	 * @param stringBuffer
	 * @param pageEncoding
	 *            if pageEncoding is null, it's utf8
	 * @return
	 */
	public int simpleGetPageContent(String urlString,
			StringBuffer stringBuffer, String pageEncoding) {
		int result = -1;
		
		try {
			HttpGet httpget = new HttpGet(urlString);
			HttpResponse response = httpClient.execute(httpget);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				InputStream instream = entity.getContent();
				try {
					int l;
					byte[] tmp = new byte[2048];
					while ((l = instream.read(tmp)) != -1) {
						stringBuffer.append(new String(tmp, 0, l, pageEncoding));
					}
				} catch (IOException e) {
					// TODO: handle exception
					e.printStackTrace();
				} finally {
					instream.close();
				}

			}
			result = response.getStatusLine().getStatusCode();
			httpget.abort();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
